package server

import (
	"context"
	"math/rand"
	"srv-server/basic/global"
	__ "srv-server/basic/proto"
	"srv-server/handler/model"
	"time"
)

type Server struct {
	__.UnimplementedSrvServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) SendSms(_ context.Context, in *__.SendSmsRequest) (*__.SendSmsResponse, error) {
	code := rand.Intn(90000) + 10000
	_, err := global.Rdb.Set(context.Background(), "SendSms"+in.Mobile+in.Source, code, time.Minute*15).Result()
	if err != nil {
		return &__.SendSmsResponse{
			Code: 500,
			Msg:  "验证码发送失败",
		}, nil
	}
	return &__.SendSmsResponse{
		Code: 200,
		Msg:  "验证码发送成功",
	}, nil
}
func (s *Server) Login(_ context.Context, in *__.LoginRequest) (*__.LoginResponse, error) {
	var u model.User

	global.DB.Where("mobile = ?", in.Mobile).Find(&u)
	if u.Id == 0 {
		u = model.User{

			Mobile: in.Mobile,
		}
		result, _ := global.Rdb.Get(context.Background(), "SendSms"+in.Mobile+"Login").Result()
		if result != in.Source {
			return &__.LoginResponse{
				Code: 200,
				Msg:  "注册验证码错误",
			}, nil
		}
		if err := global.DB.Create(&u).Error; err != nil {
			return &__.LoginResponse{
				Code: 500,
				Msg:  "注册失败",
			}, nil
		} else {
			return &__.LoginResponse{
				Code: 200,
				Msg:  "注册成功",
				Id:   int64(u.Id),
			}, nil
		}

	} else {
		result, _ := global.Rdb.Get(context.Background(), "SendSms"+in.Mobile+"Login").Result()
		if result != in.Source {
			return &__.LoginResponse{
				Code: 200,
				Msg:  "登录验证码错误",
			}, nil
		}
	}

	return &__.LoginResponse{
		Code: 200,
		Msg:  "登录成功",
		Id:   int64(u.Id),
	}, nil
}
func (s *Server) ShortPub(_ context.Context, in *__.ShortPubRequest) (*__.ShortPubResponse, error) {
	var u model.User

	global.DB.Where("id = ?", in.FormUserId).Find(&u)
	if u.Id == 0 {
		return &__.ShortPubResponse{
			Code: 500,
			Msg:  "账号错误，请重新登录",
		}, nil
	}

	var v model.ShortVideo
	video := map[string]interface{}{
		"FormUserId": in.FormUserId,
		"Title":      in.Title,
		"Desc":       in.Desc,
		"Keysword":   in.Keysword,
		"Url":        in.Url,
		"Status":     v.Status,
	}
	global.PubShortVideo(video)
	v = model.ShortVideo{

		FormUserId: in.FormUserId,
		Title:      in.Title,
		Desc:       in.Desc,
		Keysword:   in.Keysword,
		Url:        in.Url,
	}
	if err := global.DB.Create(&v).Error; err != nil {
		return &__.ShortPubResponse{
			Code: 500,
			Msg:  "短视频上传失败",
		}, nil
	}

	return &__.ShortPubResponse{
		Code: 200,
		Msg:  "短视频上传成功",
	}, nil
}
func (s *Server) ShortList(_ context.Context, in *__.ShortListRequest) (*__.ShortListResponse, error) {
	var v model.ShortVideo
	list, _ := v.List(in.FormUserId)
	var vo []*__.VideoList
	for _, video := range list {
		videoList := __.VideoList{
			FormUserId: video.FormUserId,
			Title:      video.Title,
			Desc:       video.Desc,
			Keysword:   video.Keysword,
			Url:        video.Url,
			Stats:      video.Status,
		}
		vo = append(vo, &videoList)
	}

	return &__.ShortListResponse{
		Code: 200,
		Msg:  "展示成功",
		List: vo,
	}, nil
}
func (s *Server) ShortInfo(_ context.Context, in *__.ShortInfoRequest) (*__.ShortInfoResponse, error) {
	var v []model.ShortVideo
	global.DB.Model(model.ShortVideo{}).Order("created_at desc").Where("keysword = ?", in.Keysword).Find(&v)
	var vo []*__.VideoInfo
	for _, video := range v {
		videoList := __.VideoInfo{
			Id:         video.Id,
			FormUserId: video.FormUserId,
			Title:      video.Title,
			Desc:       video.Desc,
			Keysword:   video.Keysword,
			Url:        video.Url,
			Stats:      video.Status,
		}
		vo = append(vo, &videoList)
	}
	return &__.ShortInfoResponse{
		Code: 200,
		Msg:  "展示成功",
		List: vo,
	}, nil
}
func (s *Server) ShortStatus(_ context.Context, in *__.ShortStatusRequest) (*__.ShortStatusResponse, error) {
	var v model.ShortVideo
	v = model.ShortVideo{

		Status: in.Status,
	}
	global.DB.Where("id = ?", in.Id).Updates(&v)

	return &__.ShortStatusResponse{
		Code: 200,
		Msg:  "状态修改成功",
	}, nil
}
func (s *Server) ShortDel(_ context.Context, in *__.ShortDelRequest) (*__.ShortDelResponse, error) {
	var v model.ShortVideo

	if err := global.DB.Where("id = ?", in.Id).Delete(&v).Error; err != nil {
		return &__.ShortDelResponse{
			Code: 500,
			Msg:  "删除短视频失败",
		}, nil
	}

	return &__.ShortDelResponse{
		Code: 200,
		Msg:  "删除短视频成功",
	}, nil
}
